package com.ruoyi.client.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.client.pojo.form.GoodsQueryPageForm;
import com.ruoyi.client.pojo.vo.GoodsDetailVo;
import com.ruoyi.client.pojo.vo.GoodsVo;
import com.ruoyi.client.pojo.vo.PageListVo;
import com.ruoyi.client.service.IGoodsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品管理Controller
 *
 * @author HuaQP
 * @date 2024-09-14
 */
@Anonymous
@Api(tags = "04.商品模块")
@RestController("ClientGoodsController")
@RequestMapping("/goods")
public class GoodsController extends BaseController
{
    @Resource
    private IGoodsService goodsService;

    /**
     * 查询商品管理列表
     */
    @ApiOperation("分页查询商品列表")
    @GetMapping("/list")
    public R<PageListVo<GoodsVo>> list(@Valid GoodsQueryPageForm form)
    {
        startPage();//设置分页，调用的父方法
        List<GoodsVo> list = goodsService.selectGoodsVoList(form);

        PageInfo pageInfo = new PageInfo(list);
        PageListVo<GoodsVo> pageListVo = new PageListVo<>();

        pageListVo.setTotalCount(pageInfo.getTotal());
        pageListVo.setTotalPage(pageInfo.getPages());
        pageListVo.setList(list);

        return R.ok(pageListVo);  // 接口返回
    }


    @ApiOperation(value = "查询商品详情", notes = "传参为商品id")
    @GetMapping("/{id}")
    public R<GoodsDetailVo> goodsDetail(@PathVariable @ApiParam(value = "商品id", required = true) Integer id) {

        GoodsDetailVo goodsDetailVo = goodsService.selectVoById(id);
        return R.ok(goodsDetailVo);
    }

}
